import math
import numpy as np
import torch


# 代表一种距离函数,相似度
def exp_func(x1, x2):
    return math.exp(-1 / 2 * (x1 - x2) ** 2)


def Softmax(x_test, x_train):
    # print("x_test", x_test)
    # print("x_train", x_train)
    """
    x_test tensor([0.0000, 0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000,
        4.5000])
    x_train i=1-5 tensor([0.6209, 1.7702, 3.4547, 4.0769, 4.1917])
    """
    all_list = []

    for x in x_test:
        # print("x {} 与 逐个点之间的关系".format(x))
        gx_list = []
        for xi in x_train:
            gx_list.append(exp_func(x, xi))
        xi_list = [i / sum(gx_list) for i in gx_list]
        all_list.append(xi_list)

    return torch.FloatTensor(all_list)


# 核函数1
def my_func(x1, x2):
    return 1 / math.fabs(x1 - x2)


# 核函数2
def my_func2(x1, x2):
    return 1 / ((x1 - x2) / 5) ** 2


def MySoftMax(x_test, x_train):
    # print("x_test", x_test)
    # print("x_train", x_train)
    """
    x_test tensor([0.0000, 0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000,
        4.5000])
    x_train i=1-5 tensor([0.6209, 1.7702, 3.4547, 4.0769, 4.1917])
    """
    all_list = []

    for x in x_test:
        # print("x {} 与 逐个点之间的关系".format(x))
        gx_list = []
        for xi in x_train:
            gx_list.append(my_func(x, xi))
        xi_list = [i / sum(gx_list) for i in gx_list]
        all_list.append(xi_list)

    return torch.FloatTensor(all_list)


"""
print(exp_func(0.1, 0.1))
print(exp_func(1.1,1.1))
print(exp_func(0.1, 0.2))
print(exp_func(0.2, 0.3))
print(exp_func(0.2, 0.4))
print(exp_func(0.3, 1.4))
print(exp_func(0.3, 111.4))
0.9950124791926823
0.9950124791926823
0.9801986733067553
0.5460744266397095
0.0
"""
# x_test = [0.0000, 0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000,
#           4.5000]
# x_train = [0.6209, 1.7702, 3.4547, 4.0769, 4.1917]
# softmax_of_x = Softmax(x_test, x_train)
